package com.et.u1.vm.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.et.u1.MainModel
import com.et.u1.R

abstract class BaseFragment<B : ViewBinding, M : ViewModel> : Fragment() {

    lateinit var binding: B
    lateinit var mainModel: MainModel
    lateinit var viewModel: M

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = provideViewBinding(inflater, container, savedInstanceState)
        return binding.root
    }

    abstract fun provideViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): B

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mainModel = ViewModelProvider(requireActivity()).get(MainModel::class.java)
        viewModel = provideViewModel()
        initEvent()
        // TODO
        showActionBar(true)
        showHome(true)

    }

    abstract fun provideViewModel(): M

    abstract fun initEvent()

    protected fun replaceFragment(fragment: Fragment) {
        mainModel.replaceLiveData.postValue(fragment)
    }

    fun showActionBar(b: Boolean) {
        if (b) {
            activity?.findViewById<LinearLayout>(R.id.linearLayout2)?.visibility = View.VISIBLE
            activity?.findViewById<TextView>(R.id.time)?.visibility = View.VISIBLE
        } else {
            activity?.findViewById<LinearLayout>(R.id.linearLayout2)?.visibility = View.GONE
            activity?.findViewById<TextView>(R.id.time)?.visibility = View.GONE
        }
    }


    fun showHome(b: Boolean) {
        if (b) {
            activity?.findViewById<TextView>(R.id.version)?.visibility = View.GONE
            activity?.findViewById<TextView>(R.id.home)?.visibility = View.VISIBLE
        } else {
            activity?.findViewById<TextView>(R.id.version)?.visibility = View.VISIBLE
            activity?.findViewById<TextView>(R.id.home)?.visibility = View.GONE
        }
    }

}